<template>
  <div>
    <card>
      <div style="width:100%;margin-bottom: 5px;">
        <Button type="primary" style="width:100%;" @click="recordClick" ghost>
          <h3>审批进度(点击查看详情)</h3>
        </Button>
        <span v-if="approveRecordData.length>0">
          <Table :data="approveRecordData" :columns="approveRecordColumns" :show-header="false" size="small" stripe></Table>
        </span>
      </div>
      <div style="width:100%;overflow:hidden;margin-bottom: -10px;">
          <Affix :offset-top="40">
            <div style="display:inline-block;float:left;width:49%;">
                <Spin size="large" fix v-if="spinShowSubmit"></Spin>
                <Button type="primary" @click="submitBtn">提交</Button>
                <Button type="primary" @click="backBtn">返回</Button>&nbsp;
                <Button type="primary" @click="backToPartners">退回初审</Button>
                <Button type="primary" @click="pauseBtn">暂停</Button>&nbsp;
                <Button type="primary" @click="reviewBtn">
                <Checkbox v-model="isReview" @on-change="setReviewSign"></Checkbox>已复核</Button>
            </div>
          </Affix>
      </div>
    </card>
    <div>
      <card>
        <Tabs value="name1" type="card" :animated="true" @on-click="tabOnClick">
          <TabPane label="审核页面" name="name1">
            <loan-Audit-Detail ref="loanAuditDetail" :detailParams="detailParams" @getRemarks="getRemarks"></loan-Audit-Detail>
          </TabPane>
          <TabPane label="影像资料" name="name2" :linkOption="{component:'projects/basic/pages/image/file-audit/createFileAudit',isFull:true,params:{auditParam:this.auditParam,operate:this.operate,businessDic:this.businessDic},pageTitle:this.detailParams.contractNo}">
            <fileAudit ref="fileAudit" :auditParam="auditParam" :detailParams="detailParams" :businessDic="businessDic" :operate="operate"></fileAudit>
          </TabPane>
          <TabPane label="VIN码解析" name="name3">
            <loan-Audit-Vin ref="loanAuditVin" :detailParams="detailParams"></loan-Audit-Vin>
          </TabPane>
          <TabPane label="见证人查询" name="name4">
            <loan-Audit-witness ref="loanAuditWitness" :detailParams="detailParams"></loan-Audit-witness>
          </TabPane>
        </Tabs>
      </card>
    </div>
    <Spin size="large" fix v-if="spinShow"></Spin>
    <Modal v-model="backToPartnersModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="480" :styles="{top: '180px'}">
      <div slot="footer">
        <Button type="text" @click="backToPartnersModalVisible = false">取消</Button>
        <Button type="primary" @click="backToAudit">提交</Button>
      </div>
      <div>
        <Form ref="formReview" :label-width="90" inline>
            <FormItem label="退回原因:"><Input type="textarea" :autosize="{minRows: 3}" style="width:300px;" v-model="backReason" /></FormItem>
        </Form>
      </div>
    </Modal>
      <Modal v-model="pauseModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="480" :styles="{top: '180px'}">
          <div slot="footer">
              <Button type="text" @click="pauseModalVisible = false">取消</Button>
              <Button type="primary" @click="pauseSubmit">提交</Button>
          </div>
          <div>
              <Form ref="formPause" :label-width="90" inline>
                  <FormItem label="暂停原因:"><Input type="textarea" :autosize="{minRows: 3}" style="width:300px;" v-model="pauseReason" /></FormItem>
              </Form>
          </div>
      </Modal>
      <Modal v-model="isFirstMortgageVisible" :mask-closable='false' :closable="false">
          <div class="ivu-modal-body">
              <div class="ivu-modal-confirm">
                  <div class="ivu-modal-confirm-head">
                      <div class="ivu-modal-confirm-head-icon ivu-modal-confirm-head-icon-confirm">
                          <i class="ivu-icon ivu-icon-ios-help-circle"></i>
                      </div><div class="ivu-modal-confirm-head-title">请确认</div>
                  </div>
                  <div class="ivu-modal-confirm-body">
                      <div>{{"首单未抵押!"}}</div>
                  </div>
                  <div class="ivu-modal-confirm-footer">
                      <button type="button" class="ivu-btn ivu-btn-text ivu-btn-small" @click="cancelIsFirstMortgage"><!----> <!---->取消
                      </button>
                      <button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small"  @click="getFileStatus"><!----> <!---->确定
                      </button>
                  </div>
              </div>
          </div>
          <div slot="footer">
          </div>
      </Modal>
      <Modal v-model="passLastDataModalVisible" :mask-closable='false' :closable="false">
          <div class="ivu-modal-body">
              <div class="ivu-modal-confirm">
                  <div class="ivu-modal-confirm-head">
                      <div class="ivu-modal-confirm-head-icon ivu-modal-confirm-head-icon-confirm">
                          <i class="ivu-icon ivu-icon-ios-help-circle"></i>
                      </div><div class="ivu-modal-confirm-head-title">请确认</div>
                  </div>
                  <div class="ivu-modal-confirm-body">
                      <div>{{this.passLastData+"，是否通过？"}}</div>
                  </div>
                  <div class="ivu-modal-confirm-footer">
                      <button type="button" class="ivu-btn ivu-btn-text ivu-btn-small" @click="cancelPassLastDataModal"><!----> <!---->取消
                      </button>
                      <button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small"  @click="saveSubmit"><!----> <!---->确定
                      </button>
                  </div>
              </div>
          </div>
          <div slot="footer">
          </div>
      </Modal>
      <Modal v-model="isLockVisible" :mask-closable='false' :closable="false">
          <div class="ivu-modal-body">
              <div class="ivu-modal-confirm">
                  <div class="ivu-modal-confirm-head">
                      <div class="ivu-modal-confirm-head-icon ivu-modal-confirm-head-icon-confirm">
                          <i class="ivu-icon ivu-icon-ios-help-circle"></i>
                      </div><div class="ivu-modal-confirm-head-title">请确认</div>
                  </div>
                  <div class="ivu-modal-confirm-body">
                      <div>{{"该笔放款申请暂停放款，是否通过？"}}</div>
                  </div>
                  <div class="ivu-modal-confirm-footer">
                      <button type="button" class="ivu-btn ivu-btn-text ivu-btn-small" @click="cancelIsLock"><!----> <!---->取消
                      </button>
                      <button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small"  @click="getInsuranceInfoData"><!----> <!---->确定
                      </button>
                  </div>
              </div>
          </div>
          <div slot="footer">
          </div>
      </Modal>
  </div>
</template>
<script>
import loanAuditDetail from '../loan-audit/loanAuditDetail'
import loanAuditVin from './loanAuditVin'
import loanAuditWitness from './loanAuditWitness'
import fileAudit from '@/projects/basic/pages/image/file-audit/fileAudit'
import {
  getApproveRecord,
  reviewSubmit,
  backToAudit,
  getFileStatus,
  getPassLastDate,
  getIsFirstMortgage,
    saveReviewSign,
} from '_p/afs-core-business/api/afs-case/loan-approve/loanReview'

import {savePause} from '_p/afs-core-business/api/afs-case/loan-approve/loanAudit'
export default {
  name: 'loanReview',
  components: {
    loanAuditDetail,
    loanAuditVin,
    loanAuditWitness,
    fileAudit,
  },
  // props: {
  //   detailParams: {},
  // },
  data() {
    return {
      spinShow: false,
      spinShowSubmit: false,
        operate:false,
      modalTitle: '',
      passLastData: '',
      approveRemark: '',
      passLastDataModalVisible: false,
      isLockVisible: false,
      isFirstMortgageVisible: false,
      backToAuditModalVisible: false,
      backToPartnersModalVisible: false,
        pauseModalVisible:false,
      recordDrop: false,
      isFirstMortgage:true,
        isReview:false,
      backReason: '',
        pauseReason:'',
      approveRecordData: [],
        allRecordData:[],
      auditParam: {
        busiNo: this.detailParams.contractNo,
        belongNo: this.detailParams.contractNo,
        flowNode: this.detailParams.flowNode,
        busiNode: 'loanApply',
      },
        businessDic:[{value:"loanApply",title:"放款审核"},
            {value:"loanApprove",title:"内部文件"},
            {value:"orderApply",title:"零售审批"}
        ],
      approveRecordColumns: [
        {
          title: '流程节点',
          key: 'disposeNodeName',
          render: (h, params) => {
            return h(
              'span',
              '流程节点:' + (params.row.disposeNodeName || '开始节点')
            )
          },
        },
        {
          title: '处理人员',
          key: 'disposeStaff',
          render: (h, params) => {
            return h('span', '处理人员:' + (params.row.disposeStaff || ''))
          },
        },
        {
          title: '处理命令',
          key: 'approveSuggestName',
          render: (h, params) => {
            return h(
              'span',
              '处理命令:' + (params.row.approveSuggestName || '')
            )
          },
        },
        {
          title: '处理时间',
          key: 'approveEndTime',
          render: (h, params) => {
            return h('span', '处理时间:' + (params.row.approveEndTime || ''))
          },
        },
        {
          title: '处理意见',
          key: 'approveRemark',
          render: (h, params) => {
            return h('span', '处理意见:' + (params.row.approveRemark || ''))
          },
        },
      ],
    }
  },
  beforeCreate() {
      this.detailParams = this.afs.getPageParams(this);
      console.log(this.detailParams)
  },
  mounted() {
      this.init()
  },
  methods: {
      init(){
          this.refresh()
          this.getIsFirstMortgage()
      },
      getIsFirstMortgage(){
          getIsFirstMortgage(this.detailParams).then(res=>{
              if(res.code ==='0000'){
                  this.isFirstMortgage = res.data
              }
          })
      },
    refresh() {
          if(this.detailParams.reviewSign=="yes"){
              this.isReview=true;
          }else{
              this.isReview=false;
          }
      getApproveRecord(this.detailParams).then((res) => {
        if (res.code === '0000') {
            this.allRecordData = res.data.records;
            this.setApproveRecord(this.recordDrop);
        }
      })
    },
    tabOnClick(name) {
      if (name && name === 'name3') {
        this.$refs.loanAuditVin.summaryBtn()
        //document.getElementsByTagName('DynamicLink')[0].click
      }
    },
    recordClick() {
      this.recordDrop = !this.recordDrop
        this.setApproveRecord(this.recordDrop)
    },
      setApproveRecord(val){
          if(!val){
              let records=[];
              this.allRecordData.forEach(function (v){
                  if(v.approveSuggest=="backPartners"
                      ||v.approveSuggest=="backAudit"
                      ||v.approveSuggest=="back"
                      ||v.approveSuggest=="sendBack"){
                      records.push(v);
                  }
              })
              this.approveRecordData=records;
          }else{
              this.approveRecordData=this.allRecordData;
          }
      },
    backToPartners() {
      this.modalTitle = '退回初审'
      this.backToPartnersModalVisible = true
    },
    submitBackToPartners() {},
    reviewBtn() {},
    backBtn() {
        this.whenClose();
        window.close();
    },
    whenClose(){
        this.afs.afsEmit(this,"flushData");
    },
    pauseBtn() {
        this.modalTitle = '暂停'
        this.pauseModalVisible = true
    },
      pauseSubmit() {
          let params={};
          params.contractNo=this.detailParams.contractNo;
          params.pauseDec=this.pauseReason;
          savePause(params).then(res=>{
              if (res.code === '0000'){
                  this.pauseReason="";
                  this.pauseModalVisible = false
                  this.backBtn();
              }
          });
      },
    submitBtn() {
      console.log(this.detailParams.lockDegree)
      this.spinShowSubmit = true;
      if (
        this.detailParams.isLock === 'no' ||
        this.detailParams.isLock === undefined ||
        this.detailParams.isLock === ''
      ) {
          this.getInsuranceInfoData();
      } else {
          if(this.detailParams.lockDegree === "1"){
              this.isLockVisible = true;
          } else {
              this.spinShowSubmit = false
              this.spinShow = false
              this.$Message.info('此合同已锁定,无法激活合同！')
          }
      }
    },
      cancelIsLock(){
          this.spinShowSubmit = false
          this.isLockVisible = false
      },
      getInsuranceInfoData(){
          this.isLockVisible = false;
          if(this.$refs.loanAuditDetail.insuranceInfoData.insuranceMode === 'paper'){
              //保单生效日期晚于当前日期次日时，初审复审进行提示
              var insuranceStartTime = this.$refs.loanAuditDetail.insuranceInfoData.insuranceStartTime;
              var insuranceStartTimeStr = insuranceStartTime.getFullYear() + "-" + this.p(insuranceStartTime.getMonth()+1) + "-" + this.p(insuranceStartTime.getDate());
              if (insuranceStartTimeStr > this.getDay(1)){
                  this.$Modal.confirm({
                      title: '请确认',
                      content: "保险生效日期不可晚于激活日次日，是否通过？",
                      onOk: () => {
                          //进行后续校验
                          this.getFirstMortgage();
                      },
                      onCancel:() => {
                          this.spinShowSubmit = false
                          this.$parent.back();
                      },
                  });
              }else {
                  this.getFirstMortgage();
              }
          } else {
              this.getFirstMortgage();
          }
      },
      // 获取当前时间，day为number，getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;【以此类推】
      getDay(day) {
          var today = new Date();
          var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
          today.setTime(targetday_milliseconds); //注意，这行是关键代码

          var tYear = today.getFullYear();
          var tMonth = today.getMonth();
          var tDate = today.getDate();
          tMonth = this.doHandleMonth(tMonth + 1);
          tDate = this.doHandleMonth(tDate);
          return tYear + "-" + tMonth + "-" + tDate;
      },
      p(s) {
          return s < 10 ? '0' + s : s
      },
      doHandleMonth(month) {
          var m = month;
          if (month.toString().length == 1) {
              m = "0" + month;
          }
          return m;
      },
      getFirstMortgage(){
          //进行后续校验
          if(!this.isFirstMortgage){
              this.isFirstMortgageVisible = true
          } else {
              this.getFileStatus();
          }
      },
      cancelIsFirstMortgage(){
          this.spinShowSubmit = false
          this.isFirstMortgageVisible = false
      },
    getFileStatus(){
        this.isFirstMortgageVisible = false
        this.detailParams.busiNo = this.detailParams.contractNo
        this.spinShow = true
        getFileStatus(this.detailParams).then((res) => {
            if (res.data === true) {
                this.getPassLastDate()
            } else {
                this.spinShowSubmit = false
                this.spinShow = false
                this.$Message.info('存在审核未通过的影像资料，不可提交！')
            }
        })
    },
    getPassLastDate() {
      getPassLastDate(this.detailParams).then((res) => {
        if (res.data === true) {
          this.saveSubmit()
        } else {
          this.passLastData = res.data
            this.passLastDataModalVisible = true
        }
      })
    },
    cancelPassLastDataModal(){
          this.spinShowSubmit = false
          this.passLastDataModalVisible = false
    },
    saveSubmit() {
      this.passLastDataModalVisible = false
      reviewSubmit(this.detailParams).then((res) => {
        if (res.code === '0000') {
          this.$Message.success('提交成功')
          // this.backToPartnersModalVisible = false
          // this.$emit('backGo')
          this.spinShowSubmit = false;
          this.backBtn()
        }
      })
    },
    getRemarks(val){
        this.detailParams.approveRemark = val;
    },
    backToAudit() {
      this.detailParams.approveRemark = this.backReason
      backToAudit(this.detailParams).then((res) => {
        if (res.code === '0000') {
            this.whenClose();
            window.close();
        }
      })
    },
      setReviewSign(){
          console.log(""+this.isReview);
          saveReviewSign(this.detailParams.contractNo,this.isReview).then(res=>{
              if (res.code === '0000') {
                  this.$Message.success('修改成功')
              }
          });
      },
  },
}
</script>
<style>
    .ivu-modal-footer {
        border-top: 0px solid #e8eaec;
        padding: 0px 0px 0px 0px;
        text-align: right;
    }
</style>
